﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KAImage;
using KAImage.Config;

namespace IPCPhotoEditor.ChildForms
{
    public partial class FormImageEffects : Form
    {
        private Bitmap _sourceImage = null;

        private Bitmap _destImage = null;

        public WorkImageUpdatedEventHandler WorkImageUpdated = null;

        private BlurSharpenOption _option = null;
        public BlurSharpenOption Option
        {
            get { return _option; }
        }

        protected void OnWorkImageUpdated(Bitmap image)
        {
            if (WorkImageUpdated != null)
            {
                WorkImageUpdated(this, new WorkImageUpdatedEventArgs(image));
            }
        }

        /// <summary>
        /// USM锐化程度值
        /// </summary>
        private int _usmDegree = 0;
        public int USMDegree
        {
            get
            {
                return _usmDegree;
            }
            set
            {
                if (_usmDegree != value)
                {
                    _usmDegree = value;
                    if (!this.InvokeRequired)
                    {
                        if (textBoxDegree != null)
                        {
                            textBoxDegree.Text = value.ToString();
                        }
                        if (trackBarDegree != null)
                        {
                            trackBarDegree.Value = value;
                        }                        
                    }                   
                }
            }
        }

        /// <summary>
        /// USM锐化半径
        /// </summary>
        private int _usmRadius = 0;
        public int USMRadius
        {
            get
            {
                return _usmRadius;
            }
            set
            {
                if (_usmRadius != value)
                {
                    _usmRadius = value;
                    if (!this.InvokeRequired)
                    {
                        if (textBoxRadiusUSM != null)
                        {
                            textBoxRadiusUSM.Text = value.ToString();
                        }
                        if (trackBarRadiusUSM != null)
                        {
                            trackBarRadiusUSM.Value = value;
                        }                                                      
                    }
                }
            }
        }

        /// <summary>
        /// 高斯模糊半径
        /// </summary>
        private int _gaussianRadius = 0;
        public int GaussianRadius
        {
            get
            {
                return _gaussianRadius;
            }
            set
            {
                if (_gaussianRadius != value)
                {
                    _gaussianRadius = value;
                    if (!this.InvokeRequired)
                    {
                        if (textBoxRadiusGaussian != null)
                        {
                            textBoxRadiusGaussian.Text = value.ToString();
                        }
                        if (trackBarRadiusGaussian != null)
                        {
                            trackBarRadiusGaussian.Value = value;
                        }                                                        
                    }
                }
            }
        }

        public FormImageEffects()
        {
            _option = KAConfigs.BlurSharpen;
            InitializeComponent();
        }

        public FormImageEffects(Bitmap bmp)
        {
            _sourceImage = bmp.Clone() as Bitmap;
            InitializeComponent();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (_option != null)
            {
                _option.USMDegree = USMDegree;
                _option.USMRadius = USMRadius;
                _option.GaussianRadius = GaussianRadius;
                _option.Save();
            }
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        private void buttonCancell_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }

        private void DisposeImage()
        {
            if (_sourceImage != null)
            {
                _sourceImage.Dispose();
                _sourceImage = null;
            }
            if (_destImage != null)
            {
                _destImage.Dispose();
                _destImage = null;
            }
        }

        private void UpdateImage(int mode)
        {
            if (_destImage != null)
            {
                _destImage.Dispose();
                _destImage = null;
            }
            _destImage = _sourceImage.Clone() as Bitmap;
            switch (mode)
            {
                case 0:
                    _destImage.GaussianBlur(GaussianRadius, false);
                    break;
                case 1:
                    _destImage.UsmSharpen(USMRadius, USMDegree);
                    break;            
                default:
                    break;
            }
            OnWorkImageUpdated(_destImage.Clone() as Bitmap);
        }

        private void trackBarDegree_Scroll(object sender, EventArgs e)
        {
            USMDegree = trackBarDegree.Value;
            if (_sourceImage != null)
            {
                UpdateImage(1);
            }           
        }

        private void trackBarRadiusUSM_Scroll(object sender, EventArgs e)
        {
            USMRadius = trackBarRadiusUSM.Value;
            if (_sourceImage != null)
            {
                UpdateImage(1);
            }     
        }

        private void trackBarRadiusGaussian_Scroll(object sender, EventArgs e)
        {
            GaussianRadius = trackBarRadiusGaussian.Value;
            if (_sourceImage != null)
            {
                UpdateImage(0);
            }     
        }

        private void textBoxDegree_TextChanged(object sender, EventArgs e)
        {
            USMDegree = int.Parse(textBoxDegree.Text);
            if (_sourceImage != null)
            {
                UpdateImage(1);
            }    
        }

        private void textBoxRadiusUSM_TextChanged(object sender, EventArgs e)
        {
            USMRadius = int.Parse(textBoxRadiusUSM.Text);
            if (_sourceImage != null)
            {
                UpdateImage(1);
            }    
        }

        private void textBoxRadiusGaussian_TextChanged(object sender, EventArgs e)
        {
            GaussianRadius = int.Parse(textBoxRadiusGaussian.Text);
            if (_sourceImage != null)
            {
                UpdateImage(0);
            }    
        }

        private void textBox_Validating(object sender, CancelEventArgs e)
        {
            TextBox box = sender as TextBox;
            int val, min = 0, max =255;
            if (int.TryParse(box.Text, out val))
            {
                if (box == textBoxDegree)
                {
                    max = 100;
                }
                if (val < min || val > max)
                {
                    string infoErr = string.Format("请输入一个[{0},{1}]范围内的整数.", min, max);
                    errorProvider1.SetError(box, infoErr);
                }
                else
                {
                    //Value = val;
                    errorProvider1.SetError(box, "");
                }
            }
            else
            {
                errorProvider1.SetError(box, "不是合法的数字!");
                box.Focus();
            }
        }

        private void textBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\b'
               || Char.IsDigit(e.KeyChar)
               || Char.IsControl(e.KeyChar)
               || e.KeyChar == 46)
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void FormImageEffects_FormClosing(object sender, FormClosingEventArgs e)
        {
            WorkImageUpdated = null;
            DisposeImage();
        }

        private void FormImageEffects_Load(object sender, EventArgs e)
        {
            buttonLoad.Visible = (_sourceImage != null);
            if (_option != null)
            {
                USMDegree = _option.USMDegree;
                USMRadius = _option.USMRadius;
                GaussianRadius = _option.GaussianRadius;
            }
        }

        private void buttonLoad_Click(object sender, EventArgs e)
        {
            BlurSharpenOption option = KAConfigs.BlurSharpen;
            option.Load();
            USMDegree = option.USMDegree;
            USMRadius = option.USMRadius;
            GaussianRadius = option.GaussianRadius;
        }
    }
}
